package com.example.blog.web;

import com.example.blog.annotation.LoginUser;
import com.example.blog.annotation.RequiresPermission;
import com.example.blog.constant.ApiUrlConstants;
import com.example.blog.constant.Constant;
import com.example.blog.dao.FollowInfoRepository;
import com.example.blog.dao.LikePostOrCommentRepository;
import com.example.blog.dao.PostsInfoRepository;
import com.example.blog.dto.FollowDto;
import com.example.blog.entity.FollowInfo;
import com.example.blog.entity.LikePostOrComment;
import com.example.blog.entity.PostsInfo;
import com.example.blog.enums.OperatorType;
import com.example.blog.enums.Role;
import com.example.blog.exception.NewWifiLoginException;
import com.example.blog.util.Result;
import com.example.blog.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.example.blog.constant.ApiUrlConstants.*;

/**
 * @ClassName: FollowInfoController
 * @Description:

 * @Date 2024/1/27
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/v1")
@Api(value = "收藏管理",description = "收藏管理")
public class FollowInfoController {
    @Resource
    FollowInfoRepository followInfoRepository;
    @Resource
    PostsInfoRepository postsInfoRepository;
    @ApiOperation(value = "删除收藏")
    @DeleteMapping("follow/{id}")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result delLike(@LoginUser String userId, @PathVariable String id){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        followInfoRepository.deleteById(id);
        return Result.ok();
    }
    @ApiOperation(value = "新增收藏")
    @PostMapping("/follow/{postId}")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result addLikePost(@LoginUser String userId,@PathVariable String postId,@RequestBody FollowDto followDto){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        FollowInfo followInfo = new FollowInfo();
        BeanUtils.copyProperties(followDto,followInfo);
        followInfo.setUserId(userId);
        followInfo.setPostId(postId);
        followInfo.setCreTime(new Date());
        followInfo.setUpdateTime(new Date());
        followInfoRepository.save(followInfo);
        Optional<PostsInfo> byId = postsInfoRepository.findById(postId);
        byId.get().setFollowCount(byId.get().getFollowCount()+1);
        postsInfoRepository.save(byId.get());
        return Result.ok();
    }

    /**
     * 收藏
     * @param userId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/follow")
    @ApiOperation(value = "收藏-查询列表")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result getFollowList(@LoginUser String userId,
                           @RequestParam Integer pageSize,
                           @RequestParam Integer pageNum){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        pageNum = pageNum == null || pageNum < 0 ? 0:pageNum-1;
        Pageable pageable = PageRequest.of(pageNum,pageSize); //页码：前端从1开始，jpa从0开始，做个转换
        Page<Map> page = followInfoRepository.findByUserId(userId,pageable);
        return Result.ok().setData(page);
    }
    /**
     * 收藏
     * @param userId
     * @return
     */
    @GetMapping("/follow/{postId}")
    @ApiOperation(value = "收藏-详情")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result details(@LoginUser String userId,
                           @PathVariable String postId
                           ){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        Optional<PostsInfo> byId = postsInfoRepository.findById(postId);
        return Result.ok().setData(byId.get());
    }
}
